// TodoAdapter.java
package com.example.finalproject;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class TodoAdapter extends RecyclerView.Adapter<TodoAdapter.TodoViewHolder> {
    private final ArrayList<String> todoList;
    private OnItemLongClickListener longClickListener;
    private OnItemClickListener clickListener;

    public TodoAdapter(ArrayList<String> todoList) {
        this.todoList = todoList;
    }

    @NonNull
    @Override
    public TodoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_todo, parent, false);
        return new TodoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TodoViewHolder holder, int position) {
        String todo = todoList.get(position);
        holder.todoText.setText(todo);

        // 设置优先级颜色
        if (todo.contains("[高]")) {
            holder.todoText.setTextColor(Color.RED);
        } else if (todo.contains("[中]")) {
            holder.todoText.setTextColor(Color.BLUE);
        } else if (todo.contains("[低]")) {
            holder.todoText.setTextColor(Color.GRAY);
        } else {
            holder.todoText.setTextColor(Color.BLACK);
        }

        // 设置完成状态样式
        if (todo.startsWith("[完成]")) {
            holder.todoText.setPaintFlags(holder.todoText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            holder.todoText.setPaintFlags(holder.todoText.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        }
    }

    @Override
    public int getItemCount() {
        return todoList.size();
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.longClickListener = listener;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.clickListener = listener;
    }

    public void toggleComplete(int position) {
        String item = todoList.get(position);
        if (item.startsWith("[完成]")) {
            todoList.set(position, item.substring(4));
        } else {
            todoList.set(position, "[完成]" + item);
        }
        notifyItemChanged(position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(int position);
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public class TodoViewHolder extends RecyclerView.ViewHolder {
        TextView todoText;

        public TodoViewHolder(@NonNull View itemView) {
            super(itemView);
            todoText = itemView.findViewById(R.id.tv_todo_content);

            itemView.setOnLongClickListener(v -> {
                if (longClickListener != null) {
                    longClickListener.onItemLongClick(getAdapterPosition());
                }
                return true;
            });

            itemView.setOnClickListener(v -> {
                if (clickListener != null) {
                    clickListener.onItemClick(getAdapterPosition());
                }
            });
        }
    }
}